import dayjs from "dayjs";
//表单验证
export function init_rules(obj, form) {
    let one = {
        required: true,
        message: obj.title + "不能为空！",
        trigger: obj.trigger ? obj.trigger : "blur",
    };
    let regular = {
        validator: (rule, value, callback) => {
            let str = new RegExp(`^${obj["regular"]}$`);
            let isok = str.test(value);
            if (!isok) {
                callback(new Error(obj["title"] + "输入不匹配！"));
            } else {
                callback();
            }
        },
        trigger: obj.trigger ? obj.trigger : "change",
    };
    let two = {
        validator: (rule, value, callback) => {
            let r = obj["validate"](value, form);
            if (typeof r == "string") {
                callback(new Error(r));
            } else if (typeof r == "boolean") {
                if (r) {
                    callback();
                } else {
                    callback(new Error(obj["title"] + "输入不匹配！"));
                }
            } else {
                callback();
            }
        },
        trigger: obj.trigger ? obj.trigger : "blur",
    };
    let three = {
        len: obj["len"],
        message: `${obj.title}长度必须是${obj["len"]}`,
        trigger: "blur",
    };
    let four = {
        type: "email",
        message: obj.title + "不是一个合格的邮箱地址！",
        trigger: "blur",
    };
    let range = {
        validator: (rule, value, callback) => {
            if (obj["range"]) {
                try {
                    let a = parseFloat(obj["range"][0]);
                    let b = parseFloat(obj["range"][1]);
                    if (!obj["number"]) {
                        if (value && value.length && (value.length <= b && value.length >= a)) {
                            callback();
                        } else {
                            callback(
                                new Error(obj["title"] + `长度必须在${a}-${b}之间！`)
                            );
                        }
                    } else {
                        if (value <= b && value >= a) {
                            callback();
                        } else {
                            callback(new Error(obj["title"] + `必须在${a}-${b}之间！`));
                        }
                    }
                } catch (error) {
                    callback();
                }
            } else {
                callback();
            }
        },
        trigger: obj.trigger ? obj.trigger : "change",
    };
    let max = {
        validator: (rule, value, callback) => {
            if (value && value.length && (value.length > parseFloat(obj["max"]))) {
                callback(new Error(obj["title"] + "不能大于" + obj["max"] + "位"));
            } else {
                callback();
            }
        },
        trigger: obj.trigger ? obj.trigger : "change",
    };
    let min = {
        validator: (rule, value, callback) => {
            if (obj["number"] == true) {
                if (value && value.length && (value.length < parseFloat(obj["min"]))) {
                    callback(new Error(obj["title"] + "不能小于" + obj["min"]));
                } else {
                    callback();
                }
            } else {
                callback();
            }
        },
        trigger: obj.trigger ? obj.trigger : "change",
    };
    let url = {
        type: "url",
        message: `${obj.title}不是一个合格的网址！`,
        trigger: "blur",
    };
    let temp = [];
    if (obj["regular"] !== undefined) {
        temp.push(regular);
    }
    if (obj["required"] === true) {
        temp.push(one);
    }
    if (obj["validate"] !== undefined) {
        temp.push(two);
    }
    if (obj["len"] !== undefined) {
        temp.push(three);
    }
    if (obj["email"] === true) {
        temp.push(four);
    }
    if (obj["url"] === true) {
        temp.push(url);
    }
    if (obj["range"] !== undefined) {
        temp.push(range);
    }
    if (obj["number"] && obj["max"]) {
        temp.push(max);
    }
    if (obj["number"] && obj["min"]) {
        temp.push(min);
    }
    return temp;
}


export let dateOptions = {
    shortcuts: [
        {
            text: "今日",
            onClick(picker) {
                const end = new Date();
                const start = new Date();
                picker.$emit("pick", [start, end]);
            },
        },

        {
            text: "昨日",
            onClick(picker) {
                let start = dayjs().subtract(1, "day").format("YYYY-MM-DD");
                picker.$emit("pick", [start, start]);
            },
        },
        {
            text: "本周",
            onClick(picker) {
                let start = dayjs().subtract(7, "day").format("YYYY-MM-DD");
                let end = dayjs().format("YYYY-MM-DD");
                picker.$emit("pick", [start, end]);
            },
        },
        {
            text: "本月",
            onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setDate(1);
                picker.$emit("pick", [start, end]);
            },
        },
        {
            text: "上月",
            onClick(picker) {
                const end = new Date();
                // 获取上个月的最后一天是几号day
                const day = new Date(
                    end.getFullYear(),
                    end.getMonth(),
                    0
                ).getDate();
                const enddate = new Date(
                    end.getFullYear(),
                    end.getMonth() - 1,
                    day
                );
                const start = new Date();
                start.setDate(1);
                start.setMonth(start.getMonth() - 1);
                picker.$emit("pick", [start, enddate]);
            },
        },
        {
            text: "近3月",
            onClick(picker) {
                let start = dayjs().subtract(3, "month").format("YYYY-MM-DD");
                let end = dayjs().format("YYYY-MM-DD");
                picker.$emit("pick", [start, end]);
            },
        },
        {
            text: "近半年",
            onClick(picker) {
                let start = dayjs().subtract(6, "month").format("YYYY-MM-DD");
                let end = dayjs().format("YYYY-MM-DD");
                picker.$emit("pick", [start, end]);
            },
        },
        {
            text: "近一年",
            onClick(picker) {
                let start = dayjs().subtract(1, "year").format("YYYY-MM-DD");
                let end = dayjs().format("YYYY-MM-DD");
                picker.$emit("pick", [start, end]);
            },
        },
    ],
}
